//
//  WHRDiscountCouponVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/12/3.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "WHRDiscountCouponVC.h"
#import "WHRDiscountCouponCell.h"
#import "WHRDiscountCouponModel.h"
#import "UITableView+SDAutoTableViewCellHeight.h"


@interface WHRDiscountCouponVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<WHRDiscountCouponModel *>  *modelsArray;



@end

@implementation WHRDiscountCouponVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self creatView];
    [self setlayout];

    self.view.backgroundColor = [UIColor whiteColor];
    
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestList];
        
    }];
    
    
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestList];
    }];
    
    LKEmptyViewConfig *config = [LKEmptyViewConfig setupEmptyViewConfigForTableView:self.tableView];
    config.message = @"暂无数据";
    
    [self.tableView.mj_header beginRefreshing];
    
}




- (void)creatView {
    /** 表头创建 */
    self.tableView = [[UITableView alloc]init];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[WHRDiscountCouponCell class] forCellReuseIdentifier:[WHRDiscountCouponCell reuseID]];
    [self.view addSubview:self.tableView];
    
}



- (void)setlayout {
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .widthIs(Wi)
    .heightIs(TableViewHeight);
    
    
}



#pragma mark - requestNetwork

- (void)requestList {
    
    MBProgressHUD *hud = [WHRNetwork showHudAtView:self.view message:@"正在加载..." isTextOnly:NO];
    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //params[@"shopId"] = @1;
    params[@"shopId"] = self.shopId;
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_ActivityPrefecture, URL_ActivityPrefecture_getCouponListByMmallCoupon) success:^(id respose) {
        
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.modelsArray = [WHRDiscountCouponModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"seckillListVOs"]];
            [self.tableView reloadData];
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
            
        }
        
        [hud hideAnimated:YES];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    
}


#pragma mark - Action


#pragma mark - tableViewDelegate & tableViewDatasorce



//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelsArray.count;
}

/** cell的高度 */

/** cell 自适应第二步 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[WHRDiscountCouponCell class] contentViewWidth:SCREEN_WIDTH];
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    WHRDiscountCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:[WHRDiscountCouponCell reuseID] forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.model  = self.modelsArray[indexPath.row];
    
    return cell;
    
    
}





@end

